package logic

import (
	"com-man-sys1/goodsrpc/goodsrpc"
	"com-man-sys1/goodsrpc/internal/svc"
	"com-man-sys1/models/mysql"
	"context"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 登录注册方法
func (l *GoodsAddLogic) GoodsAdd(in *goodsrpc.GoodsAddRequest) (*goodsrpc.GoodsAddResponse, error) {
	// todo: add your logic here and delete this line

	goods := &mysql.Goods{
		GoodsName:  in.GoodsName,
		GoodsPrice: float64(in.GoodsPrice),
		GoodsCode:  in.GoodsCode,
		FromPeople: in.FromPeople,
		UserId:     int(in.UserId),
	}

	err := goods.FirstGoodsByName(mysql.DB, in.GoodsName)
	if err == nil {
		return nil, errors.New("该商品已存在")
	}

	err = goods.GoodsAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("添加失败")
	}

	return &goodsrpc.GoodsAddResponse{
		GoodsId: int64(goods.ID),
	}, nil
}
